package org.example.javafxdemo;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;

/**
 * 上下分页
 * @Author 56465
 * @Create 2024/7/29 17:54
 */
public class MarkdownPager extends Application {

    private WebView leftPage;
    private WebView rightPage;
    private String markdownContent;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Markdown Pager");

        // Initialize WebView components for left and right pages
        leftPage = new WebView();
        rightPage = new WebView();

        // Create a parser and renderer for Markdown
        Parser parser = Parser.builder().build();
        HtmlRenderer renderer = HtmlRenderer.builder().build();

        // Sample Markdown content
        markdownContent = "# Page 1\n\nThis is the content of page 1.\n\n# Page 2\n\nThis is the content of page 2.";

        // Split the Markdown content into pages (for simplicity, assume 2 pages)
        String[] pages = markdownContent.split("\n\n# ");
        String page1 = "# " + pages[0];
        String page2 = "# " + pages[1];

        // Parse and render each page
        Node page1Node = parser.parse(page1);
        String page1Html = renderer.render(page1Node);
        leftPage.getEngine().loadContent(page1Html);

        Node page2Node = parser.parse(page2);
        String page2Html = renderer.render(page2Node);
        rightPage.getEngine().loadContent(page2Html);

        // Create buttons for navigation
        Button prevButton = new Button("Previous");
        prevButton.setOnAction(e -> {
            // Logic to show previous page
        });

        Button nextButton = new Button("Next");
        nextButton.setOnAction(e -> {
            // Logic to show next page
        });

        // Layout setup
        HBox buttonBox = new HBox(prevButton, nextButton);
        buttonBox.setSpacing(10);
        buttonBox.setPadding(new Insets(10));

        VBox root = new VBox(buttonBox, leftPage, rightPage);
        root.setSpacing(10);
        root.setPadding(new Insets(10));

        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
